<template>
  <div class="container">
    <el-card>
      <div class="header">
        <div>
          <span style="font-weight: 700; color: #606266">关键字</span>
          <el-input
            v-model="keyword"
            placeholder="根据编号搜索"
            style="width: 300px; margin-left: 20px"
          ></el-input>
        </div>
        <div>
          <el-button @click="clear">清除</el-button>
          <el-button type="primary" @click="search">搜索</el-button>
        </div>
      </div>
      <div class="total">
        <i class="el-icon-info"></i>
        <span>数据一共{{ total }}条</span>
      </div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="编号" width="210"> </el-table-column>
        <el-table-column label="题型" width="80">
          <template v-slot="scope">
            {{ getQuestionType(scope.row.questionType) }}
          </template>
        </el-table-column>
        <el-table-column label="题目编号" width="200">
          <template v-slot="scope">
            <div v-for="(item, index) in scope.row.questionIDs" :key="index">
              <p @click="preview(item.id)">{{ item.number }}</p>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="addTime" label="录入时间"> </el-table-column>
        <el-table-column prop="totalSeconds" label="答题时间(s)">
        </el-table-column>
        <el-table-column prop="accuracyRate" label="正确率(%)">
        </el-table-column>
        <el-table-column prop="userName" label="录入人"> </el-table-column>
        <el-table-column label="操作" width="80">
          <template slot-scope="scope">
            <i class="el-icon-delete" @click="del(scope.row)"></i>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="pages.page"
          :page-size="pages.pagesize"
          :page-sizes="[5, 10, 15, 20]"
          layout="prev, pager, next, sizes, jumper"
          :total="total"
          background
        >
        </el-pagination>
      </div>
      <diaLogDetail
        :dialogVisible.sync="dialogVisible"
        :id="id"
        v-if="dialogVisible"
      ></diaLogDetail>
    </el-card>
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions'
import diaLogDetail from './components/diaLogDetail'
export default {
  components: {
    diaLogDetail
  },
  data () {
    return {
      keyword: '',
      total: 1,
      tableData: [],
      pages: {
        page: 1,
        pagesize: 10,
        keyword: ''
      },
      dialogVisible: false
    }
  },
  created () {
    this.getQuestionsList()
  },
  methods: {
    search () {
      this.getQuestionsList()
    },
    clear () {
      this.keyword = ''
      this.getQuestionsList()
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.pages.pagesize = val
      this.getQuestionsList()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.pages.page = val
      this.getQuestionsList()
    },
    async getQuestionsList () {
      // 页面刷新获取数据
      const res = await randoms({
        page: this.pages.page,
        pagesize: this.pages.pagesize,
        keyword: this.keyword
      })
      this.tableData = res.data.items
      this.total = res.data.counts
    },
    getQuestionType (n) {
      // 处理题型数据
      if (n === '1') {
        return '单选'
      } else if (n === '2') {
        return '多选'
      } else {
        return '简答'
      }
    },
    del (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          if (this.tableData.length === 1) {
            this.pages.page--
          }
          await removeRandoms(row)
          this.getQuestionsList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async preview (id) {
      this.dialogVisible = true
      this.id = id
    }
  }
}
</script>

<style scoped lang="scss">
.container {
  padding: 20px;
  .header {
    display: flex;
    justify-content: space-between;
  }
  .total {
    background-color: #f4f4f5;
    margin-top: 20px;
    color: #909399;
    height: 30px;
    line-height: 30px;
    margin-bottom: 15px;
    i {
      margin: 0 20px 0 10px;
    }
    span {
      font-size: 14px;
    }
  }
  .el-icon-delete {
    color: red;
    border: 1px solid red;
    padding: 4px;
    border-radius: 50%;
    font-size: 12px;
  }
  .block {
    text-align: right;
    margin-top: 20px;
  }
  p {
    color: #7f99ff;
    cursor: pointer;
  }
}
</style>
